//
//  TFFenPromptTool.swift
//  TalkfunSDKDemo
//
//  Created by 莫瑞权 on 2022/5/5.
//  Copyright © 2022 Talkfun. All rights reserved.
//

import UIKit
import TFAnimatedImage
import AFNetworking
public class TFFenPromptTool: TalkfunHitTestView {
    
    //画板宽度
    @objc public var whiteboardWidth:CGFloat = 0
    //网络错误
    @objc public let networkError = UIView()
    @objc public let layerView = UIImageView()
    @objc public let content = UILabel()
    //4g网络
    @objc public let mobileNetwork = UIView()
    @objc public let mobileNetworkContent = UILabel()
    //网络不稳定
    @objc public let networkInstability = UIView()
    @objc public let expression = UIImageView()
    @objc public let networkInstabilityContent = UILabel()
    //加载中
    @objc public let loading = UIView()
    @objc public let loadingNetworkInstabilityContent = UILabel()
    @objc public let loadingExpression = TFAnimatedImageView()

    @objc public let backgroundImage = TFbackgroundImage()
    @objc public var iphoneXpacing:CGFloat = 0

    override init(frame: CGRect) {
        super.init(frame: frame)
     
     
        self.addSubview(self.backgroundImage)
        
        let networkReachabilityManager =     AFNetworkReachabilityManager.shared()
         
        networkReachabilityManager.startMonitoring()
        //网络更改
        NotificationCenter.default.addObserver(self, selector: #selector(self.ReachabilityDidChange), name: Notification.Name(NSNotification.Name.AFNetworkingReachabilityDidChange.rawValue), object: nil)
       
    
        self.backgroundColor = UIColor.white
        self.networkError.isHidden = true
        self.networkError.backgroundColor = UIColor(red: 1, green: 0.93, blue: 0.93, alpha: 1)
       
        self.addSubview(self.networkError)
        self.layerView.image = UIImage.init(named: "HTLive.bundle/netfail")
        self.networkError.addSubview(self.layerView)
 
        
        self.content.font = UIFont.systemFont(ofSize: 15)
        
        self.content.text = "网络连接失败，请检查您的网络设置"
        self.content.backgroundColor = UIColor.clear
        self.content.textColor = UIColor(red: 0.32, green: 0.2, blue: 0.03, alpha: 1)
        self.networkError.addSubview(self.content)
    
    
        //网络不稳定
        self.networkInstability.isHidden = true
        self.networkInstability.backgroundColor = UIColor(red: 0, green: 0, blue: 0, alpha:     0.6)
        self.addSubview(self.networkInstability)
    
         //表情
        self.expression.isHidden = true
        self.expression.backgroundColor = UIColor.clear
       
        self.expression.image =  UIImage.init(named: "HTLive.bundle/netbuwending")
        self.networkInstability.addSubview(self.expression)
    
        
        self.networkInstabilityContent.text = "网络不稳定"
        self.networkInstabilityContent.font = UIFont.systemFont(ofSize: 15)
        self.networkInstabilityContent.textAlignment = .center
        self.networkInstabilityContent.textColor = UIColor(red: 1, green: 1, blue: 1, alpha:  1)
        self.networkInstability.addSubview(self.networkInstabilityContent)
    
      //4G网络
        self.mobileNetwork.isHidden = true
        self.mobileNetwork.backgroundColor = UIColor(red: 0, green: 0, blue: 0, alpha:     0.6)
        self.mobileNetwork.layer.cornerRadius = 20
    
    
        self.mobileNetworkContent.text = "当前网络切换为4g，注意流量使用哦"
        self.mobileNetworkContent.font = UIFont.systemFont(ofSize: 15)
        self.mobileNetworkContent.textAlignment = .center
        self.mobileNetworkContent.textColor =  UIColor(red: 1, green: 1, blue: 1, alpha:  1)
        self.mobileNetworkContent.backgroundColor = UIColor.clear
        
        self.mobileNetwork.addSubview(self.mobileNetworkContent)
        self.addSubview(self.mobileNetwork)
    
    
       //加载中
        self.loading.layer.cornerRadius = 6;
        self.loading.backgroundColor = UIColor(red: 0, green: 0, blue: 0, alpha:     0.6)
        self.addSubview(self.loading)
    
        //表情
        self.loadingExpression.backgroundColor = UIColor.clear
    
        self.loading.addSubview(self.loadingExpression)
    

        let retinaPath = Bundle.main.path(forResource: "TF_turnAround", ofType: "gif")
        if retinaPath != nil {
            if let data = NSData(contentsOfFile: retinaPath! as String) {
                self.loadingExpression.animatedImage =  TFAnimatedImage.init(animatedGIFData: data as Data)
            }
        }

        
        self.loadingNetworkInstabilityContent.text = "加载中"
        self.loadingNetworkInstabilityContent.textAlignment
        = .center
       self.loadingNetworkInstabilityContent.font =  UIFont.systemFont(ofSize: 15)
       self.loadingNetworkInstabilityContent.textColor =  UIColor(red: 1, green: 1, blue: 1, alpha:  1)
        self.loading.addSubview(self.loadingNetworkInstabilityContent)
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    public override func layoutSubviews() {
        super.layoutSubviews()
       
        var UIScreenHeight = UIScreen.main.bounds.height
        let UIScreenWidth = self.whiteboardWidth
    //屏幕宽度
    if ( UIScreen.main.bounds.width < UIScreen.main.bounds.height) {
        UIScreenHeight = UIScreen.main.bounds.width;
        
    }
    
        self.networkError.frame = CGRect(x: self.iphoneXpacing, y: 0, width:  self.whiteboardWidth, height: 35)

        
        let backgroundImageH = 84.5+32.5
        self.backgroundImage.frame = CGRect(x: (self.frame.size.width-144.5)/2, y: (self.frame.size.height-backgroundImageH)/2, width: 200, height: backgroundImageH)
  
        self.layerView.frame = CGRect(x: 26.0, y: 7.5, width: 20, height: 20)

        let contentX = self.layerView.frame.origin.x +  self.layerView.frame.size.width + 15
        self.content.frame = CGRect(x: contentX, y: 0, width: 254.0, height: self.networkError.frame.size.height)
      
    
    
        let networkInstabilityW = 130.0;
        let networkInstabilityH = 110.0;
        let networkInstabilityX = (UIScreenWidth  - networkInstabilityW) / 2;
        let networkInstabilityY = (UIScreenHeight - networkInstabilityH) / 2;
    
       // 网络不稳定
        self.networkInstability.frame = CGRect(x: networkInstabilityX, y: networkInstabilityY, width: networkInstabilityW, height: networkInstabilityH)
        self.expression.frame = CGRect(x: 40, y: 10, width: 50, height: 50)

        self.networkInstabilityContent.frame = CGRect(x: 26, y: 79, width: 78, height: 16)
      
      //加载中
        self.loading.frame = self.networkInstability.frame
        self.loadingExpression.frame = self.expression.frame
        self.loadingNetworkInstabilityContent.frame = CGRect(x: 26, y: 79, width: 78, height: 16)
      
    
        let mobileNetworkWidth = 310.0;
        let mobileNetworkHeight = 40.0;
        let mobileNetworkX = ( UIScreenWidth - mobileNetworkWidth) / 2;
        let mobileNetworkY = (UIScreenHeight - mobileNetworkHeight) / 2;
    //4G网络
        self.mobileNetwork.frame = CGRect(x: mobileNetworkX, y: mobileNetworkY, width: mobileNetworkWidth, height: mobileNetworkHeight)
        self.mobileNetworkContent.frame = CGRect(x: 0, y: 0, width: self.mobileNetwork.frame.size.width, height: self.mobileNetwork.frame.size.height)
     
    }
    @objc func ReachabilityDidChange(notification:NSNotification)
    {

        let params = notification.userInfo! as NSDictionary
        let Reacha =  params[AFNetworkingReachabilityNotificationStatusItem] as! NSNumber
           let  ReachabilityStatusUnknown          = -1
           let  ReachabilityStatusNotReachable     = 0
           let  ReachabilityStatusReachableViaWWAN = 1
           let  ReachabilityStatusReachableViaWiFi = 2
        
        let  status  = NSInteger(Reacha.intValue)

        if (status==ReachabilityStatusReachableViaWWAN) {
            self.mobileNetwork.isHidden = false
            
            DispatchQueue.main.asyncAfter(deadline: DispatchTime.now()+1.0) {
                self.mobileNetwork.isHidden = true;
            }
            //自带
            self.networkError.isHidden = true
            self.mobileNetwork.isHidden = false
            
            DispatchQueue.main.asyncAfter(deadline: DispatchTime.now()+3.0) {
                
                self.mobileNetwork.isHidden = true
            }
        }else if (status==ReachabilityStatusReachableViaWiFi)
        {
            //wift
            self.networkError.isHidden = true
            self.mobileNetwork.isHidden = true
            self.mobileNetwork.isHidden = true
        }else if (status==ReachabilityStatusUnknown)
        {
            //未知网络
            self.networkError.isHidden = false
            self.mobileNetwork.isHidden = true
            self.mobileNetwork.isHidden = true
        }else if (status==ReachabilityStatusNotReachable)
        {
            //没有网络
            self.networkError.isHidden = false
            self.mobileNetwork.isHidden = true
            //4G
            self.mobileNetwork.isHidden = true
        }
        
    }
    //更新语言
    @objc public func upDataLanguage()
    {
        self.content.text = "网络连接失败，请检查您的网络设置"
        self.networkInstabilityContent.text = "网络不稳定"
        self.loadingNetworkInstabilityContent.text = "加载中"
    }
   
    @objc public func destroy()
   {
       NotificationCenter.default.removeObserver(self)
       self.mobileNetwork.removeFromSuperview()
    }

}
